package com.project.easy.common.util.io;

import com.project.easy.common.util.exception.print.PrintException;

import java.awt.image.BufferedImage;
import java.io.Closeable;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Objects;
/**
 * @author GreatorMan.cool
 * @copyright: Copyright (c) 2020
 * @company: 扬起科技有限公司
 * @date 2021/11/23 21:42
 * @description:
 */
public final class CloseResouce {
    /**
     * 私有构造方法
     */
    private CloseResouce() {
    }

    /**
     * 批量关闭数据流
     * @param res 资源
     */
    public static void closes(Closeable... res) {
        if (res.length > 0) {
            for (Closeable closeable: res) {
                try {
                    closeable.close();
                } catch (Exception e) {
                    PrintException.print(e);
                }
            }
        }
    }
    /**
     * 关闭数据流
     * @param outputStream 输入流
     */
    public static   void close(OutputStream outputStream) {
        if (!Objects.isNull(outputStream)) {
            try {
                outputStream.flush();
                outputStream.close();
            } catch (Exception e) {
                PrintException.print(e);
            }
        }
    }
    /**
     * 关闭输入流
     * @param inputStream 输入流
     */
    public static   void close(InputStream inputStream) {
        if (!Objects.isNull(inputStream)) {
            try {
                inputStream.close();
            } catch (Exception e) {
                PrintException.print(e);
            }
        }
    }
    /**
     * 输出流
     * @param image 图片流
     */
    public static   void close(BufferedImage image) {
        if (!Objects.isNull(image)) {
            try {
                image.flush();
            } catch (Exception e) {
                PrintException.print(e);
            }
        }
    }


}
